package com.woniuxy.java106crm.controller;

import com.woniuxy.java106crm.annotation.RequireMethod;
import com.woniuxy.java106crm.entity.Perms;
import com.woniuxy.java106crm.request.ResponseResult;
import com.woniuxy.java106crm.service.PermsService;
import com.woniuxy.java106crm.vo.PermsVo;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "权限管理模块API文档")
@RestController
@RequestMapping("/perms")
public class PermsController {
    @Resource
    private PermsService permsService;
    //角色分配权限 查询全部权限
    @RequireMethod("查询全部权限")
    @GetMapping("/queryPerms")
    public ResponseResult queryPerms(){
        List<Perms> permsList = permsService.queryPerms();
        return new ResponseResult(ResponseResult.OK, permsList);
    }

    /**
     * 根据id查询权限
     */
    @RequireMethod("根据id查询权限")
    @GetMapping("/selectById/{id}")
    public ResponseResult selectById(@PathVariable("id") int id){
        return new ResponseResult(ResponseResult.OK, permsService.selectById(id));
    }

    /**
     * 根据id修改权限信息
     */
    @RequireMethod("根据id修改权限信息")
    @PostMapping("/updatePerms")
    public ResponseResult updatePerms(@RequestBody Perms perms){
        return new ResponseResult<>(ResponseResult.OK, permsService.updatePerms(perms));
    }

    /**
     * 用户添加权限
     */

    @RequireMethod("用户添加权限")
    @PostMapping("/addPerms")
    public ResponseResult addPerms(@Valid @RequestBody PermsVo permsVo){
        System.out.println(permsVo);
        if(permsVo.getParentId() == null){
            permsVo.setParentId(0);
        }
        return new ResponseResult<>(ResponseResult.OK, permsService.addPerms(permsVo));
    }

}
